package com.yunlin.bean;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.os.Parcel;
import android.os.Parcelable;

import com.yunlin.blecontroler.BleDeviceUtils;
import com.yunlin.utils.BteKeyUtils;
import com.yunlin.utils.ConvertUtils;


/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2018/7/19
       Time     :  20:50
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class BleDevice implements Parcelable{
    private BluetoothDevice bluetoothDevice;
    private ScanResult scanResult;
    
    
    private boolean isEnable;
    private String macAddress;
    private String k0;
    private String k1;
    
    public String getDeviceName(){
        return deviceName;
    }
    
    public void setDeviceName(String deviceName){
        this.deviceName=deviceName;
    }
    
    private String deviceName;
    
    
    public String getMacAddress(){
        return macAddress;
    }
    
    public void setMacAddress(String macAddress){
        this.macAddress=macAddress;
    }
    
    public String getK0(){
        return k0;
    }
    
    public void setK0(String k0){
        this.k0=k0;
    }
    
    public String getK1(){
        return k1;
    }
    
    public void setK1(String k1){
        this.k1=k1;
    }
    
    public BluetoothDevice getBluetoothDevice(){
        return bluetoothDevice;
    }
    
    public void setBluetoothDevice(BluetoothDevice bluetoothDevice){
        this.bluetoothDevice=bluetoothDevice;
    }
    
    public ScanResult getScanResult(){
        return scanResult;
    }
    
    public void setScanResult(ScanResult scanResult){
        this.scanResult=scanResult;
    }
    
    
    public BleDevice(BluetoothDevice bluetoothDevice, ScanResult result){
        this.bluetoothDevice=bluetoothDevice;
        this.scanResult=result;
        
        byte[] scanRecord=result.getScanRecord().getBytes();
        
        String scanRecordStr=ConvertUtils.bytesToStringData(scanRecord);
        String k0=BteKeyUtils.getK0Str(scanRecordStr);
        String k1=BteKeyUtils.getK1Str(scanRecordStr);
        String lockAddress=BleDeviceUtils.converRawMacToHexMacAddress(bluetoothDevice);
        this.k0=k0;
        this.k1=k1;
        this.macAddress=lockAddress;
        this.deviceName=bluetoothDevice.getName();
        this.isEnable = true;
    }
    
    protected BleDevice(Parcel in){
        this.bluetoothDevice=in.readParcelable(BluetoothDevice.class.getClassLoader());
        this.scanResult=in.readParcelable(ScanResult.class.getClassLoader());
    }
    
    
    public boolean isEnable(){
        return isEnable;
    }
    
    public void setEnable(boolean enable){
        isEnable=enable;
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int flags){
        parcel.writeParcelable(bluetoothDevice, flags);
        parcel.writeParcelable(scanResult, flags);
    }
    
    @Override
    public int describeContents(){
        return 0;
    }
    
    public static final Creator<BleDevice> CREATOR=new Creator<BleDevice>(){
        @Override
        public BleDevice createFromParcel(Parcel in){
            return new BleDevice(in);
        }
        
        @Override
        public BleDevice[] newArray(int size){
            return new BleDevice[size];
        }
    };
}
